package com.uit.gis.ui;

import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.EmptyBorder;

import com.uit.gis.controller.MainController;
import com.uit.gis.util.Language;
import com.uit.gis.util.Util;

public class MainFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	PanelDatabase paneltabDatabase;
	PanelDrawing panelDrawing;
	PanelAbout panelAbout;
	MainController controller;

	/**
	 * Create the frame.
	 */
	public MainFrame(MainController controller) {
		/*
		 * 
		 */
		this.controller = controller;
		try {
			this.controller.databaseConfig.readConfigFile();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		paneltabDatabase = new PanelDatabase(controller);
		panelDrawing = new PanelDrawing(controller);
		panelAbout = new PanelAbout(controller);
		this.controller.setPanelDatabase(paneltabDatabase);
		this.controller.setPanelDrawing(panelDrawing);

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1000, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new GridLayout(1, 1));
		setContentPane(contentPane);
		this.setTitle(Language.APP_NAME);
		ImageIcon imageIcon = Util.getApplicationIcon();
		if (imageIcon != null)
			this.setIconImage(imageIcon.getImage());
		this.setResizable(false);

		JTabbedPane tabbedPane = new JTabbedPane();
 
		tabbedPane.addTab(Language.TAB_DATABASE, null, paneltabDatabase, "");
		tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
		tabbedPane.setToolTipTextAt(0, Language.TAB_DATABASE);

		tabbedPane.addTab(Language.TAB_DRAWING, null, panelDrawing, "");
		tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
		tabbedPane.setToolTipTextAt(1, Language.TAB_DRAWING);

		tabbedPane.addTab(Language.TAB_ABOUT, null, panelAbout, "");
		tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);
		tabbedPane.setToolTipTextAt(2, Language.TAB_ABOUT);
		contentPane.add(tabbedPane);
		final MainController mainController = controller;
		
		this.addWindowListener( new WindowListener() {
			
			@Override
			public void windowOpened(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowIconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeiconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeactivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				
				mainController.getPanelDatabase().saveConfigFile();
				mainController.closeDatabase();
			}
			
			@Override
			public void windowClosed(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowActivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
	}
	
	 

	protected JComponent makeTextPanel(String text) {
		JPanel panel = new JPanel(false);
		JLabel filler = new JLabel(text);
		filler.setHorizontalAlignment(JLabel.CENTER);
		panel.setLayout(new GridLayout(1, 1));
		panel.add(filler);
		return panel;
	}
}
